
package com.joysuch.wwyt.core.common.ajax;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.joysuch.wwyt.core.enums.Status;
import org.springframework.data.domain.Page;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResultBean {

    public static final int SUCCESS_CODE = 0;

    private int code;
    private String msg;
    private Object data;
    private Long length;

    public ResultBean(int resultCode, String msg, Object data) {
        super();
        this.code = resultCode;
        this.msg = msg;
        this.data = data;
    }

    public ResultBean() {
        super();
    }

    /**
     * 返回一个带错误提示的错误结果
     *
     * @param code
     *            错误码
     * @param message
     *            错误提示
     * @return 错误结果
     */
    public static ResultBean wrapErrorResult(int code, String message) {
        ResultBean r = new ResultBean();
        r.setCode(code);
        r.setMsg(message);
        return r;
    }

    /**
     * 返回一个默认错误结果
     *
     * @param message
     *            错误提示
     * @return 错误结果
     */
    public static ResultBean wrapErrorResultDefault(String message) {
        ResultBean r = new ResultBean();
        r.setCode(-90);
        r.setMsg(message);
        return r;
    }

    /**
     * 返回一个带数据的成功结果
     *
     * @param data
     *            数据
     * @return 结果
     */
    public static ResultBean wrapSuccessfulResult(Object data) {
        ResultBean result = new ResultBean();
        result.data = data;
        result.code = SUCCESS_CODE;
        return result;
    }

    /**
     * 返回默认成功结果，不包含数据
     *
     * @return 默认成功结果
     */
    public static ResultBean defaultSuccessResult() {
        ResultBean result = new ResultBean();
        result.code = SUCCESS_CODE;
        return result;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int resultCode) {
        this.code = resultCode;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Long getLength() {
        return length;
    }

    public void setLength(Long length) {
        this.length = length;
    }

    public static ResultBean success(Object data) {
        return new ResultBean(SUCCESS_CODE, null, data);
    }

    public static ResultBean success(Object data, String msg) {
        return new ResultBean(SUCCESS_CODE, msg, data);
    }

    public static ResultBean fail(int code, String msg, Object data) {
        return new ResultBean(code, msg, data);
    }

    public static ResultBean fail(int code, String msg) {
        return new ResultBean(code, msg, null);
    }

    public static ResultBean fail(Status status) {
        return new ResultBean(status.getCode(), status.getMsg(), null);
    }

    public static Map<String, Object> successInMap(Object data) {
        return toMap(SUCCESS_CODE, null, data);
    }

    public static Map<String, Object> successInMap(Object data, String msg) {
        return toMap(SUCCESS_CODE, msg, data);
    }

    public static Map<String, Object> failInMap(int code, String msg, Object data) {
        return toMap(code, msg, data);
    }

    public static Map<String, Object> failInMap(int code, String msg) {
        return toMap(code, msg, null);
    }

    static Map<String, Object> toMap(int code, String msg, Object data) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", code);
        if (msg != null) {
            map.put("msg", msg);
        }
        if (data != null) {
            map.put("data", data);
        }
        return map;
    }

    public static ResultBean pageData(List list, long total) {
        ResultBean rb = new ResultBean();
        rb.setData(list);
        rb.setLength(total);
        rb.setCode(SUCCESS_CODE);
        return rb;
    }

    public static ResultBean pageData(Page p) {
        ResultBean rb = new ResultBean();
        rb.setData(p.getContent());
        rb.setLength(p.getTotalElements());
        rb.setCode(SUCCESS_CODE);
        return rb;
    }

}
